package com.smart.manager.pop.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.pop.entities.body.RBasicPoster;
import com.smart.manager.pop.entities.models.PopBasicPoster;
import com.smart.manager.pop.entities.models.query.QPopBasicPoster;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class PopPosterRepository {

    private final AsyncService asyncService;


    public Page<PopBasicPoster> pageList(RBasicPoster.SearchBody search) {

        QPopBasicPoster query = new QPopBasicPoster();
        Opt.ofBlankAble(search.getTitle()).ifPresent(x -> query.title.contains(x));
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().id.desc();
        return new Page<PopBasicPoster>().load(query.findPagedList());
    }

    /**
     * 添加
     *
     * @param form 形式
     */
    public void add(RBasicPoster.EditBody form) {
        PopBasicPoster paymentWechat = BeanUtil.copyProperties(form, PopBasicPoster.class);
        paymentWechat.save();
    }

    /**
     * 更新
     *
     * @param id   id
     * @param form 形式
     */

    public void update(Long id, RBasicPoster.EditBody form) {
        PopBasicPoster record = new QPopBasicPoster().id.eq(id).findOne();
        Assert.notNull(record, "海报不存在");
        BeanUtil.copyProperties(form, record);
        record.update();
        asyncService.getActivityAttendUpdate(record);
    }


    /**
     * 批量删除
     *
     * @param idList id列表
     * @return int
     */
    public int delByIds(List<Long> idList) {

        return DB.deleteAll(PopBasicPoster.class, idList);

    }


    public List<SelectOption> getOptions() {
        QPopBasicPoster place = new QPopBasicPoster();
        return place.findStream()
                .map(x -> new SelectOption().setLabel(x.getTitle()).setValue(String.valueOf(x.getId())))
                .collect(Collectors.toList());
    }
}
